using System;
using System.Collections.Generic;
using System.IO;

namespace Cinemacenter.Shared
{
	public class DBHelper
	{
		MarquecineDatabase db;
		protected static string DbLocation;
		protected static DBHelper Me;
		const int DATABASE_VERSION = 2;

		static DBHelper ()
		{
			Me = new DBHelper ();
		}

		protected DBHelper ()
		{
			// set the db location
			DbLocation = DatabaseFilePath;
			
			// instantiate the database	
			db = new MarquecineDatabase (DbLocation);
		}

		public static string DatabaseFilePath {
			get { 
				var sqliteFilename = string.Format ("com.ldb.marquecine.db{0}", DATABASE_VERSION);

#if NETFX_CORE
                var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, sqliteFilename);
#else

#if SILVERLIGHT
				// Windows Phone expects a local path, not absolute
	            var path = sqliteFilename;
#else

#if __ANDROID__
				// Just use whatever directory SpecialFolder.Personal returns
				string libraryPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
#else
				// we need to put in /Library/ on iOS5.1 to meet Apple's iCloud terms
				// (they don't want non-user-generated data in Documents)
				string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
				string libraryPath = Path.Combine (documentsPath, "../Library/"); // Library folder
#endif
				var path = Path.Combine (libraryPath, sqliteFilename);
#endif		

#endif
				return path;	
			}
		}

		public static City GetCity (int id)
		{
			return Me.db.GetItem<City> (id);
		}

		public static Movie GetMovie (int id)
		{
			return Me.db.GetItem<Movie> (id);
		}

		public static UpcomingMovie GetUpcoming (int id)
		{
			return Me.db.GetItem<UpcomingMovie> (id);
		}

		public static List<City> GetCities ()
		{
			return new List<City> (Me.db.GetItems<City> ());
		}

		public static List<Movie> GetMovies ()
		{
			return new List<Movie> (Me.db.GetItems<Movie> ());
		}

		public static List<UpcomingMovie> GetUpcomingMovies ()
		{
			return new List<UpcomingMovie> (Me.db.GetItems<UpcomingMovie> ());
		}

		public static int SaveCity (City item)
		{
			return Me.db.SaveItem<City> (item);
		}

		public static int SaveMovie (Movie item)
		{
			return Me.db.SaveItem<Movie> (item);
		}

		public static int SaveUpcoming (UpcomingMovie item)
		{
			return Me.db.SaveItem<UpcomingMovie> (item);
		}

		public static int SaveCities (List<City> items)
		{
			return Me.db.InsertAll (items);
		}

		public static int SaveMovies (List<Movie> items)
		{
			return Me.db.InsertAll (items);
		}

		public static int SaveUpcomingMovies (List<UpcomingMovie> items)
		{
			return Me.db.InsertAll (items);
		}

		public static int DeleteCity (int id)
		{
			return Me.db.DeleteItem<City> (id);
		}

		public static int DeleteMovie (int id)
		{
			return Me.db.DeleteItem<Movie> (id);
		}

		public static int DeleteUpcoming (int id)
		{
			return Me.db.DeleteItem<UpcomingMovie> (id);
		}

		public static int DeleteCities ()
		{
			return Me.db.DeleteItems<City> ();
		}

		public static int DeleteMovies ()
		{
			return Me.db.DeleteItems<Movie> ();
		}

		public static int DeleteUpcomingMovies ()
		{
			return Me.db.DeleteItems<UpcomingMovie> ();
		}
	}
}

